package com.ch.clinking.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ch.clinking.ClinkingApplication;
import com.ch.clinking.R;
import com.ch.clinking.Utils.NetUtils.DoCallBack;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.bean.User;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;

public class EditUserInfoActivity extends AppCompatActivity {

    private static final int PICK_IMAGE_REQUEST = 1;

//    private EditText editTextOldPassword;
    private EditText editTextNewPassword;
    private EditText editTextConfirmPassword;
    private Button buttonChangePassword;
    private ImageView imageViewAvatar;
    private Button buttonChangeAvatar;

    private ClinkingApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_edit_user_info);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        app = ClinkingApplication.getInstance();

        // 初始化视图
//        editTextOldPassword = findViewById(R.id.editTextOldPassword);
        editTextNewPassword = findViewById(R.id.editTextNewPassword);
        editTextConfirmPassword = findViewById(R.id.editTextConfirmPassword);
        buttonChangePassword = findViewById(R.id.buttonChangePassword);
//        imageViewAvatar = findViewById(R.id.imageViewAvatar);
//        buttonChangeAvatar = findViewById(R.id.buttonChangeAvatar);

        // 按钮点击事件 - 更改密码
        buttonChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePassword();
            }
        });

//        // 按钮点击事件 - 更改头像
//        buttonChangeAvatar.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                openImageChooser();
//            }
//        });
    }

    private void changePassword() {
        // 获取输入的数据
//        String oldPassword = editTextOldPassword.getText().toString();
        String newPassword = editTextNewPassword.getText().toString();
        String confirmPassword = editTextConfirmPassword.getText().toString();

        // 验证输入是否为空
        if (TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(confirmPassword)) {
            Toast.makeText(this, "信息未填写完整", Toast.LENGTH_SHORT).show();
            return;
        }

        // 验证新密码和确认密码是否匹配
        if (!newPassword.equals(confirmPassword)) {
            Toast.makeText(this, "新密码两次输入不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        // 更新密码
        updatePassword(newPassword);
        Toast.makeText(this, "密码更改成功", Toast.LENGTH_SHORT).show();

        // 可以在此处进行更多操作，如关闭 Activity 或导航到另一个页面
        finish();
    }

    private boolean validateOldPassword(String oldPassword) {
        // 假设我们验证旧密码的逻辑，例如从数据库或共享偏好中获取旧密码
        // 此处简单返回 true，表示旧密码验证成功
        return true;
    }

    private void updatePassword(String newPassword) {
        app.user.setPassword(newPassword);

        // 更新User
        HashMap<String, Object> addMember = new HashMap<>();


        addMember.put("user", app.user);

        String addMemberJson = new Gson().toJson(addMember);


        OkHttpUtil.getInstance().doPostJson("/user/changePassword", new DoCallBack() {
            @Override
            public void onSuccess(String result) {
                Log.d("changePassword", "result");

                EditUserInfoActivity.this.finish();

            }

            @Override
            public void onError(Exception e) {
                Toast.makeText(EditUserInfoActivity.this, "网络错误,请稍后再试", Toast.LENGTH_SHORT).show();
            }
        }, addMemberJson);
        // 在此处实现更新密码的逻辑，例如保存到数据库或共享偏好
        // 可以根据你的应用需求来实现
    }

    private void openImageChooser() {
        // 创建一个选择图片的Intent
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri imageUri = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                imageViewAvatar.setImageBitmap(bitmap);

                // 此处可以将图片保存到本地或上传到服务器
                saveAvatar(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(this, "Failed to load image", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void saveAvatar(Bitmap bitmap) {
        // 在此实现保存头像的逻辑，例如将其保存到本地存储或上传到服务器
        // 这里你可以根据你的应用需求实现保存头像的功能
    }
}
